﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using DataAccess;
using System.Globalization;

namespace InsuranceVehicle
{
    public partial class PolicyInsurance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                panelInsuranceInfo.Visible = false;
                if (Session["CustomerID"] == null)
                {
                    panelError.Visible = true;
                    panelPolicy.Visible = false;
                    lblErrLogin.Text = "You must be Login to Buy Insurance Online";

                }
                else
                {
                    panelError.Visible = false;
                    panelPolicy.Visible = true;
                }
                addListItem();
            }

        }

        private void addListItem()
        {
            for (int i = 1; i <= 10; i++)
            {
                cbbDuration.Items.Add(i.ToString());
            }

            VehicleDataAccess vda = new VehicleDataAccess();
            List<VehicleInfo> listVehicle = vda.getListVehicleType();
            foreach (var item in listVehicle)
            {
                cbbVehicleType.Items.Add(item.Model);
            }

            InsuranceTypeAccess db = new InsuranceTypeAccess();
            List<InsuranceTypeInfo> listInsu = db.getListOfInsuranceType();
            foreach (var item in listInsu)
            {
                cbbInsuType.Items.Add(item.InsuranceName);

            }

            int curYear = DateTime.Now.Year;
            for (int i = curYear - 20; i <= curYear; i++)
            {
                cbbYearOfVehicle.Items.Add(i.ToString());
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            Page.Validate();
            lblError.Visible = false;

            if (IsValid)
            {
                if (cbbVehicleType.SelectedIndex == 0)
                {
                    lblError.Visible = true;
                    lblError.Text = "Please, choose your vehicle";
                }
                else if (cbbYearOfVehicle.SelectedIndex == 0)
                {
                    lblError.Visible = true;
                    lblError.Text = "Please, choose year when you buy this vehicle";
                }
                else
                {
                    PersonalVehicleDataAccess pvda = new PersonalVehicleDataAccess();
                    PersonalVehicleInfo per = new PersonalVehicleInfo();

                    VehicleDataAccess vda = new VehicleDataAccess();
                    VehicleInfo vehi = new VehicleInfo();
                    vehi = vda.getVehicleTypeByModel(cbbVehicleType.SelectedItem.ToString());

                    per.CustomerID = Convert.ToInt32(Session["CustomerID"].ToString());
                    per.VehicleID = vehi.VehicleID;
                    per.ControlPlate = txtControlPlate.Text;

                    int yearBuy = Convert.ToInt32(cbbYearOfVehicle.SelectedItem.ToString());
                    int currYear = DateTime.Today.Year;
                    per.UsedYear = currYear - yearBuy;
                    per.Price = Convert.ToSingle(txtPrice.Text);

                    pvda.InsertVehicle(per);
                    panelVehicleInfo.Visible = false;
                    panelInsuranceInfo.Visible = true;


                }

            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lblErrChooseInsurance.Visible = false;
            lblErrChooseYear.Visible = false;
            lblErrConfirm.Visible = false;
            lblNotice.Visible = false;

            bool flag1 = true;
            bool flag2 = true;
            bool flag3 = true;
            if (cbbInsuType.SelectedIndex == 0)
            {
                lblErrChooseInsurance.Visible = true;
                lblErrChooseInsurance.Text = "Please, choose Insurance";
                flag1 = false;
            }

            if (cbbDuration.SelectedIndex == 0)
            {
                lblErrChooseYear.Visible = true;
                lblErrChooseYear.Text = "Please, Insurance time";
                flag2 = false;

            }

            if (checkBoxAccept.Checked == false)
            {
                lblErrConfirm.Visible = true;
                lblErrConfirm.Text = "Please, You must read and accept provisions and content of Insurance";
                flag3 = false;
            }

            InsurancePolicyAccess ipa = new InsurancePolicyAccess();
            InsurancePolicyInfo policy = new InsurancePolicyInfo();

            PersonalVehicleDataAccess pvda = new PersonalVehicleDataAccess();
            PersonalVehicleInfo pvi = new PersonalVehicleInfo();
            pvi = pvda.getPerVehicleByControlPlate(txtControlPlate.Text);

            InsuranceTypeAccess ita = new InsuranceTypeAccess();
            InsuranceTypeInfo itp = new InsuranceTypeInfo();

            if (flag1 && flag2 && flag3)
            {

                itp = ita.getInsuranceByName(cbbInsuType.SelectedItem.ToString());

                policy.CustomerID = Convert.ToInt32(Session["CustomerID"].ToString());
                policy.PerVehicleID = pvi.PerVehicleID;
                policy.InsuranceID = itp.InsuranceID;
                policy.PolicyDate = Convert.ToDateTime(DateTime.Today.ToShortDateString());
                policy.PolicyDuration = cbbDuration.SelectedItem.ToString();
                policy.InsuredAmount = Convert.ToSingle(lblAmountNumber.Text);

                lblErrAccName.Visible = false;
                lblErrAccNum.Visible = false;
                bool flagAccName = true;
                bool flagAccNum = true;
                bool flagStatus = true;
                if (rdPayOnline.Checked)
                {
                    if (txtAccName.Text.Equals(""))
                    {
                        lblErrAccName.Visible = true;
                        lblErrAccName.Text = "Please, enter your Bank Account name";
                        txtAccName.Focus();
                        flagAccName = false;
                    }

                    if (txtAccNum.Text.Equals(""))
                    {
                        lblErrAccNum.Visible = true;
                        lblErrAccNum.Text = "Please, enter your Bank Account number";
                        txtAccName.Focus();
                        flagAccNum = false;
                    }

                    if (flagAccName && flagAccNum)
                    {
                        PaymentService.Payment service = new PaymentService.Payment();
                        bool result = service.checkPayment(txtAccNum.Text, txtAccName.Text);
                        if (result == true)
                        {
                            policy.Status = "paid";
                            flagStatus = true;
                        }
                        else
                        {
                            lblErrAccNum.Visible = true;
                            lblErrAccNum.Text = "Payment failed.<br/> Please, check your Infomations.";
                            flagStatus = false;
                        }
                    }
                }
                else
                {
                    policy.Status = "not";

                }

                if (flagAccName && flagAccNum && flagStatus)
                {

                    CustomerInfo c = (CustomerInfo)Session["Customer"];
                    string email = c.Email;
                    string subject = "Vehicle Insurance Online";

                    string mess = ipa.AddPolicyInsurance(policy) + "<br/> Please check your email to get your Policy infomation";
                    lblNotice.Visible = true;
                    lblNotice.Text = mess;
                    btnSubmit.Enabled = false;

                    string custName = c.Firstname;
                    string insuType = cbbInsuType.SelectedItem.ToString();
                    string vehicle = cbbVehicleType.SelectedItem.ToString();
                    string vehicleNumber = txtControlPlate.Text;
                    string duration = cbbDuration.SelectedItem.ToString();
                    string amount = lblAmount.Text;
                    itp = ita.getInsuranceByName(cbbInsuType.SelectedItem.ToString());
                    policy = ipa.getPolicyByVehicleAndInsuranceType(pvi.PerVehicleID, itp.InsuranceID);
                    string polID = policy.PolicyID.ToString();

                    string content = "Dear " + custName + ",<br/><br/>Today: " + DateTime.Now.ToString()
                        + "<br/> You have bought <b style=\"font-size:15px\">" + insuType
                        + "</b> for <b style=\"font-size:15px\">" + vehicle
                        + " </b> what have control plate is <b style=\"font-size:15px\"> " + vehicleNumber
                        + "</b> in <b style=\"font-size:15px\"> " + duration
                        + "</b> year with Price: <b style=\"font-size:15px\">" + amount
                        + "</b><br/>From Our Vehicle Insurance Online System.<br/<br/>Your PolicyID: <b style=\"font-size:15px\">" 
                        + polID +"</b>"
                        + "</b> <br/> <h3>Thank you for using our service!</h3>";
                    GoogleMail.Send(email, subject, content);

                    
                }

            }

        }

        protected override void InitializeCulture()
        {
            CultureInfo lang = new CultureInfo("en-US");
            System.Threading.Thread.CurrentThread.CurrentCulture = lang;
            System.Threading.Thread.CurrentThread.CurrentUICulture = lang;

        }

        public string setPrice(string price)
        {
            CultureInfo culInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
            NumberFormatInfo numberFormat = culInfo.NumberFormat;
            double temp = Convert.ToDouble(price);
            string newPrice = temp.ToString("c", culInfo);
            return newPrice;
        }

        protected void cbbInsuType_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblErrChooseInsurance.Visible = false;
            string selectedInsurance = cbbInsuType.SelectedItem.ToString();
            InsuranceTypeAccess db = new InsuranceTypeAccess();
            InsuranceTypeInfo i = new InsuranceTypeInfo();
            i = db.getInsuranceByName(selectedInsurance);
            int duration;
            if (i != null)
            {
                if (cbbDuration.SelectedIndex != 0)
                {

                    duration = Convert.ToInt32(cbbDuration.SelectedItem.ToString());
                    lblAmount.Text = setPrice((i.UnitPrice * duration).ToString());
                    lblAmountNumber.Text = (i.UnitPrice * duration).ToString();
                }
                showDescription.Text = i.Description + "\n" + "Unit Price(1 year): " + i.UnitPrice;

            }
            else
            {
                lblErrChooseInsurance.Visible = true;
                lblErrChooseInsurance.Text = "Please, choose Insurance";
                showDescription.Text = "";
            }
        }


        public void setAmount()
        {
            string selectedInsurance = cbbInsuType.SelectedItem.ToString();
            int duration;

            InsuranceTypeAccess db = new InsuranceTypeAccess();
            InsuranceTypeInfo i = new InsuranceTypeInfo();
            i = db.getInsuranceByName(selectedInsurance);
            if (i != null)
            {
                duration = Convert.ToInt32(cbbDuration.SelectedItem.ToString());
                lblAmount.Text = setPrice((i.UnitPrice * duration).ToString());
                lblAmountNumber.Text = (i.UnitPrice * duration).ToString();
            }

        }

        protected void cbbDuration_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cbbDuration.SelectedIndex != 0 && cbbInsuType.SelectedIndex != 0)
            {
                lblErrChooseYear.Visible = false;
                setAmount();
            }
        }

        protected void rdPayOnline_CheckedChanged(object sender, EventArgs e)
        {
            panelPayOnline.Visible = true;
        }

        protected void rdpayOffline_CheckedChanged(object sender, EventArgs e)
        {
            panelPayOnline.Visible = false;
        }

    }
}